﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;
using System.Windows.Forms;

namespace JYH.Framework
{
    /// <summary>
    /// Config配置文件公共帮助类
    /// </summary>
    public class ConfigHelper
    {
        /// <summary>
        /// 根据Key获取Value值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            return ConfigurationManager.AppSettings[key].ToString().Trim();
        }

        public static string GetConnectionString(string key)
        {
            if (CheckConnectionStringKey(key))
            {
                return ConfigurationManager.ConnectionStrings[key].ConnectionString.ToString();
            }
            else
            {
                return null;
            }
        }

        public static string GetProviderName(string key)
        {
            if (CheckConnectionStringKey(key))
            {
                return ConfigurationManager.ConnectionStrings[key].ProviderName.ToString();
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 检测是否存在Key
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool CheckAppSettingKey(string key)
        {
            bool result = false;
            string[] keys = ConfigurationManager.AppSettings.AllKeys;
            for (int i = 0; i < keys.Length; i++)
            {
                if (keys[i] == key)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// AppSetting节操作(Key存在则修改/不存在则新增)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetAppSettingKey(string key, string value)
        {
            if (CheckAppSettingKey(key))
            {
                ConfigurationManager.AppSettings.Set(key, value);
            }
            else
            {
                ConfigurationManager.AppSettings.Add(key, value);
            }
        }

        /// <summary>
        /// 检测ConnectionString节Key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool CheckConnectionStringKey(string key)
        {
            bool result = false;
            for (int i = 0; i < ConfigurationManager.ConnectionStrings.Count; i++)
            {
                if (ConfigurationManager.ConnectionStrings[i].Name == key)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// ConnectionString节操作(Key存在则修改/不存在则新增)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="connectionString"></param>
        /// <param name="providerName"></param>
        public static void SetConnectionStringKey(string key, string connectionString, string providerName)
        {
            if (CheckConnectionStringKey(key))
            {
                ConfigurationManager.ConnectionStrings[key].ConnectionString = connectionString;
                ConfigurationManager.ConnectionStrings[key].ProviderName = providerName;
            }
            else
            {
                ConnectionStringSettings setting = new ConnectionStringSettings();
                setting.Name = key;
                setting.ProviderName = providerName;
                setting.ConnectionString = connectionString;
                ConfigurationManager.ConnectionStrings.Add(setting);
            }
        }

        /// <summary>
        /// 根据Key修改Value(自定义Xml配置文件)
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetXmlValue(string key, string value)
        {
            string fileName = HttpContext.Current.Server.MapPath("/XmlConfig/Config.xml");
            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
            xDoc.Load(fileName);
            System.Xml.XmlNode xNode;
            System.Xml.XmlElement xElem1;
            System.Xml.XmlElement xElem2;
            xNode = xDoc.SelectSingleNode("//appSettings");

            xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + key + "']");
            if (xElem1 != null)
            {
                xElem1.SetAttribute("value", value);
            }
            else
            {
                xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("key", key);
                xElem2.SetAttribute("value", value);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(fileName);
        }

        /// <summary>
        /// 修改元素值(未找到相应的键，会新建一个元素)
        /// </summary>
        /// <param name="AppKey">元素名</param>
        /// <param name="AppValue">元素值</param>
        public static void SetAppSetttingValue(string AppKey, string AppValue)
        {
            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
            xDoc.Load(Application.ExecutablePath + ".config");

            System.Xml.XmlNode xNode;
            System.Xml.XmlElement xElem1;
            System.Xml.XmlElement xElem2;
            xNode = xDoc.SelectSingleNode("//appSettings");

            xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
            if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
            else
            {
                xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("key", AppKey);
                xElem2.SetAttribute("value", AppValue);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(Application.ExecutablePath + ".config");
        }

        /// <summary>
        /// 修改元素值(未找到相应的键，会新建一个元素)
        /// </summary>
        /// <param name="FileName">config配置文件</param>
        /// <param name="AppKey">元素名</param>
        /// <param name="AppValue">元素值</param>
        public static void SetAppSetttingValue(string FileName, string AppKey, string AppValue)
        {
            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
            xDoc.Load(FileName);

            System.Xml.XmlNode xNode;
            System.Xml.XmlElement xElem1;
            System.Xml.XmlElement xElem2;
            xNode = xDoc.SelectSingleNode("//appSettings");

            xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
            if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
            else
            {
                xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("key", AppKey);
                xElem2.SetAttribute("value", AppValue);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(FileName);
        }

        /// <summary>
        /// 修改元素值(未找到相应的键，会新建一个元素)
        /// </summary>
        /// <param name="ConnKey">元素名</param>
        /// <param name="ConnValue">元素值</param>
        /// <param name="providerName"></param>
        public static void SetConnectionStringsValue(string ConnKey, string ConnValue, string providerName)
        {
            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
            xDoc.Load(Application.ExecutablePath + ".config");

            System.Xml.XmlNode xNode;
            System.Xml.XmlElement xElem1;
            System.Xml.XmlElement xElem2;
            xNode = xDoc.SelectSingleNode("//connectionStrings");

            xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@name='" + ConnKey + "']");
            if (xElem1 != null)
            {
                xElem1.SetAttribute("connectionString", ConnValue);
                xElem1.SetAttribute("providerName", providerName);
            }
            else
            {
                xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("name", ConnKey);
                xElem2.SetAttribute("connectionString", ConnValue);
                xElem2.SetAttribute("providerName", providerName);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(Application.ExecutablePath + ".config");
        }

        /// <summary>
        /// 修改元素值(未找到相应的键，会新建一个元素)
        /// </summary>
        /// <param name="FileName">config配置文件</param>
        /// <param name="ConnKey">元素名</param>
        /// <param name="ConnValue">元素值</param>
        /// <param name="providerName"></param>
        public static void SetConnectionStringsValue(string FileName, string ConnKey, string ConnValue, string providerName)
        {
            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
            xDoc.Load(FileName);

            System.Xml.XmlNode xNode;
            System.Xml.XmlElement xElem1;
            System.Xml.XmlElement xElem2;
            xNode = xDoc.SelectSingleNode("//connectionStrings");

            xElem1 = (System.Xml.XmlElement)xNode.SelectSingleNode("//add[@name='" + ConnKey + "']");
            if (xElem1 != null)
            {
                xElem1.SetAttribute("connectionString", ConnValue);
                xElem1.SetAttribute("providerName", providerName);
            }
            else
            {
                xElem2 = xDoc.CreateElement("add");
                xElem2.SetAttribute("name", ConnKey);
                xElem2.SetAttribute("connectionString", ConnValue);
                xElem2.SetAttribute("providerName", providerName);
                xNode.AppendChild(xElem2);
            }
            xDoc.Save(FileName);
        }

    }
}
